package world.criver.mm_vworld_server.function;

import org.springframework.stereotype.Component;
import world.criver.mm_vworld_server.data.daAdmin;
import world.criver.mm_vworld_server.data.daCache;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.Base64;
import java.util.Random;

@Component
public class funAdminVerifyCode {
    private char[] chars={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
                          'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
                          '0','1','2','3','4','5','6','7','8','9'};

    public String createCode(){
        String code="";
        Random ran=new Random();
        while(code.length()<32)code+=chars[ran.nextInt(chars.length)];
        code+="?"+ LocalDateTime.now().format(daCache.dtf_0);
        return Base64.getEncoder().encodeToString(code.getBytes(StandardCharsets.UTF_8));
    }
    public boolean test(String code){
        if(daAdmin.verifyCode==null || daAdmin.verifyCode.isEmpty())return false;
        if(code==null || code.isEmpty())return false;
        return daAdmin.verifyCode.equals(code);
    }
}
